import unittest

from pyramid.configuration import Configurator
from pyramid import testing


class CreativeCommonsTests(unittest.TestCase):
    def setUp(self):
        self.config = Configurator()
        self.config.begin()

    def tearDown(self):
        self.config.end()

    def test_cc_classes(self):
        from ampersands.core.vocabulary import cc_getLicenseClasses
        self.assertEqual(cc_getLicenseClasses(),
                         [('recombo', 'Sampling'),
                          ('mark', 'Public Domain Mark'), ('zero', 'CC0'),
                          ('publicdomain', 'Public Domain'),
                          ('standard', 'Creative Commons')])

    def test_cc_license(self):
        from ampersands.core.vocabulary import cc_getLicense
        self.assertEqual(cc_getLicense('standard'),
                         {'__keys__': ['commercial', 'derivatives', 'jurisdiction'],
                          'derivatives': {'enum': {'y': 'Yes', 'sa':
                                                   'ShareAlike', 'n': 'No'},
                                          'type': 'enum',
                                          'description': 'The licensor permits'
                                          ' others to copy, distribute and '
                                          'transmit only unaltered copies of '
                                          'the work \xe2\x80\x94 not '
                                          'derivative works based on it.',
                                          'label': 'Allow modifications of '
                                          'your work?'},
                          'jurisdiction': {'enum':
                                           {'': 'Generic',
                                            'ch': 'Switzerland',
                                            'co': 'Colombia',
                                            'cn': 'China Mainland',
                                            'cl': 'Chile', 'ca': 'Canada',
                                            'it': 'Italy', 'ec': 'Ecuador',
                                            'cz': 'Czech Republic',
                                            'ar': 'Argentina',
                                            'au': 'Australia', 'at': 'Austria',
                                            'in': 'India', 'es': 'Spain',
                                            'gr': 'Greece',
                                            'pr': 'Puerto Rico',
                                            'nl': 'Netherlands',
                                            'pt': 'Portugal',
                                            'no': 'Norway', 'tw': 'Taiwan',
                                            'scotland': 'UK: Scotland',
                                            'nz': 'New Zealand',
                                            'lu': 'Luxembourg',
                                            'th': 'Thailand', 'pe': 'Peru',
                                            'ph': 'Philippines',
                                            'ro': 'Romania', 'pl': 'Poland',
                                            'be': 'Belgium', 'fr': 'France',
                                            'bg': 'Bulgaria',
                                            'dk': 'Denmark', 'hr': 'Croatia',
                                            'de': 'Germany', 'jp': 'Japan',
                                            'gt': 'Guatemala',
                                            'za': 'South Africa',
                                            'hk': 'Hong Kong', 'br': 'Brazil',
                                            'fi': 'Finland', 'hu': 'Hungary',
                                            'kr': 'Korea', 'my': 'Malaysia',
                                            'rs': 'Serbia',
                                            'us': 'United States',
                                            'mt': 'Malta', 'si': 'Slovenia',
                                            'mk': 'Macedonia',
                                            'uk': 'UK: England & Wales',
                                            'sg': 'Singapore',
                                            'mx': 'Mexico', 'se': 'Sweden',
                                            'il': 'Israel'},
                                           'type': 'enum',
                                           'description': 'If you desire a '
                                           'license governed by the Copyright '
                                           'Law of a specific jurisdiction, '
                                           'please select the appropriate '
                                           'jurisdiction.',
                                           'label':
                                           'Jurisdiction of your license:'},
                          'commercial': {'enum': {'y': 'Yes', 'n': 'No'},
                                         'type': 'enum',
                                         'description': "The licensor permits "
                                         'others to copy, distribute and '
                                         'transmit the work. In return, '
                                         'licensees may not use the work for '
                                         'commercial purposes \xe2\x80\x94 '
                                         'unless they get the '
                                         'licensor\'s permission.',
                                         'label':
                                         'Allow commercial uses of your work?'}
                          }
                         )

    def test_cc_formgen(self):
        from ampersands.core.schemas import cc_license_form
        from ampersands.core.vocabulary import cc_getLicense
        self.assertEqual(cc_license_form(cc_getLicense('standard')),
                         [('recombo', 'Sampling'),
                          ('mark', 'Public Domain Mark'), ('zero', 'CC0'),
                          ('publicdomain', 'Public Domain'),
                          ('standard', 'Creative Commons')])

    def test_cc_renderlicense(self):
        from ampersands.core.schemas import render_license
        self.assertEqual(render_license({'license_class': u'standard',
                                         'jurisdiction': u'au',
                                         'commercial': u'y',
                                         'derivatives': u'sa'}),
                                        {'license-name':
                                         'Attribution-ShareAlike 3.0 Australia'})
